package org.example.springbootexperiment03.controller;

import org.example.springbootexperiment03.pojo.Region;
import org.example.springbootexperiment03.service.IRegionService;
import org.example.springbootexperiment03.vo.LatestRegionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by bothin
 * At 2020/5/7 14:22
 * Description:
 */
@RestController
@RequestMapping("/data/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @GetMapping("all")
    public List<Region> getALLRegions(){
        return this.regionService.getALLRegions();
    }

    @GetMapping("find/country")
    public List<LatestRegionVO> getByCountryName(@RequestParam(value = "name") String countryName,
                                                 @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                 @RequestParam(value = "size", defaultValue = "10") Integer size){
        //若是countryName为空，则返回全部数据
        if (StringUtils.isEmpty(countryName)){
            return (List<LatestRegionVO>) this.regionService.getLatestRegions().get("globalData");
        }
        return this.regionService.getByCountryName(countryName);
    }

    @GetMapping("find/id/{id}")
    public Map<String,Object> getById(@PathVariable("id") Integer id){
        return this.regionService.getById(id);
    }

    @GetMapping("/latest")
    public Map<String,Object> getLatestRegions(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "size", defaultValue = "10") Integer size){
        return this.regionService.getLatestRegions();
    }


}
